package com.maoning.ware.service.impl;

import com.maoning.ware.vo.SkuHasStockVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoning.common.utils.PageUtils;
import com.maoning.common.utils.Query;

import com.maoning.ware.dao.WareInfoDao;
import com.maoning.ware.entity.WareInfoEntity;
import com.maoning.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        final QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
        final String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.and((w) -> {
                w.eq("id", key)
                        .or().like("name", key)
                        .or().like("address", key)
                        .or().like("areacode", key);
            });
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVo> getHasStock(List<Long> skuIds) {
        final List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            final SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            Long count = baseMapper.getHasStockBySkuId(skuId);
            skuHasStockVo.setSkuId(skuId);
            skuHasStockVo.setHasStock(count==null? false :count >0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}